/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bean;

import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import utilitaire.Calculs;
import utilitaire.GestionSQL;

/**
 *
 * @author romain_lou
 */
public class Coordonne {
    private Double x;
    private Double y;
    private boolean isCalcul;

    public Coordonne(Double x, Double y) {
        this.x = x;
        this.y = y;
    }

    public void calcul(){
        if(isCalcul == false){
            Calculs c = new Calculs(x, y);

            this.x = c.getLatitude();
            this.y = c.getLongitude();
        }
        isCalcul = true;
    }


    public Double getX() {
        return x;
    }

    public void setX(Double x) {
        this.x = x;
    }

    public Double getY() {
        return y;
    }

    public void setY(Double y) {
        this.y = y;
    }

    void save(int idParcelle) {
        this.calcul();
        int idCoordonnee = idCoordonnee(idParcelle);

        if(idCoordonnee == 0 && idParcelle != 0){

        String requete = "INSERT INTO coordonnee (id_parcelle ,latitude ,longitude)VALUES (";
        requete += "'"+idParcelle+"', ";
        requete += "'"+getX()+"', ";
        requete += "'"+getY()+"') ";

        utilitaire.Log.getLog().ecrireSansTerminal("Creation Coordonnee pour "+idParcelle);

        
        Statement statement;
            try {
                statement = (Statement) GestionSQL.getConnection().getCon().createStatement();

                statement.executeUpdate(requete);

            } catch (ClassNotFoundException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            } catch (InstantiationException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            } catch (IllegalAccessException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            } catch (SQLException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            }

        }
    }

    private int idCoordonnee(int _idParcelle){
            Statement statement = null;
                int id = 0;
            try {
                statement = (Statement) GestionSQL.getConnection().getCon().createStatement();
            } catch (ClassNotFoundException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            } catch (InstantiationException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            } catch (IllegalAccessException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            } catch (SQLException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            }

            ResultSet resultSet = null;
            try {
                resultSet = statement.executeQuery("select * from coordonnee where id_parcelle = '"+_idParcelle+"' "
                    + "AND latitude = '"+getX()+"' AND longitude = '"+getY()+"'");
                while (resultSet.next()) {
                    id = resultSet.getInt("id_coord");
                }


            } catch (SQLException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            }

            return id;
        }

    

}
